<?php


namespace App\Repositories;


use App\Models\PackageStock as Model;
use App\Traits\Logs;
use LTools\Repositories\RepositoryAbstract;

class PackageStock extends RepositoryAbstract
{
    use Logs;
    /**
     * PackageStock constructor.
     * @param Model $model
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    /**
     * @param array $attributes
     * @return bool|\Illuminate\Database\Eloquent\Model|void
     * @author luffyzhao@vip.126.com
     */
    public function create(array $attributes = [])
    {
        /** @var Model $model */
        $model = $this->model->create($attributes);
        if($model->getAttribute('type') === 0){
            $model->package()->increment('stock', $model->getAttribute('stock'));
            $this->recordLogs($model->package, '包材入仓:' . $model->getAttribute('stock'));
        }else{
            $model->package()->decrement('stock', $model->getAttribute('stock'));
            $this->recordLogs($model->package, '包材核销:' . $model->getAttribute('stock'));
        }

        return $model;
    }
}